<template>
  <a-modal
    title="修改随访计划"
    :width="600"
    :visible.sync="visible"
    :body-style="{'padding-top': '20px', paddingBottom: '80px' }"
    @cancel="close"
    :footer="null"
  >
    <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 17 }" layout="horizontal">
      <a-form-item label="计划时间">
        <a-date-picker v-decorator="['date', config]" show-time format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="计划内容">
        <a-textarea
          v-decorator="['info', { rules: [{ required: true, message: '打卡内容不能为空!' }] }]"
          placeholder="请输入打卡内容"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-item>
    </a-form>
    <div
      class="absolute right-0 bottom-0 w-full border-t border-[#e9e9e9] px-4 py-[10px] bg-white text-left z-1 flex justify-center"
    >
      <a-button :style="{ marginRight: '20px' }" class="w-[200px]" @click="close">取消</a-button>
      <a-button
        :loading="loading"
        class="w-[200px]"
        type="primary"
        @click.native.prevent="submit()"
      >
        <span v-if="!loading">提交修改</span>
        <span v-else>提交中...</span>
      </a-button>
    </div>
  </a-modal>
</template>

<script>
import axios from 'axios'
import moment from 'moment'
import { editFollowupPlan } from '@/api/index'
import draggable from 'vuedraggable'

export default {
  components: { draggable },
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      form: this.$form.createForm(this),
      config: {
        rules: [
          { type: 'object', required: true, message: '计划时间不可为空！' },
        ],
      },
      visible: false,
      dataLoading: false,
      loading: false,
      detail: {},
    }
  },
  mounted() {},
  methods: {
    /**
     *
     * @param {*} data 数据
     */
    async open(data = {}) {
      this.visible = true
      try {
        this.dataLoading = true
        this.detail = { ...data }
        await this.$nextTick()
        // console.log('data.DpfpCreatetime', data)
        this.form.setFieldsValue({
          info: data.DpfpInfo,
          date: moment(data.DpfpCreatetime, 'YYYY-MM-DD'),
        })
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.dataLoading = false
      }
    },
    submit() {
      const {
        form: { validateFields },
      } = this
      const id = this.detail.Id
      this.loading = true
      validateFields(['info', 'date'], { force: true }, async (err, values) => {
        if (!err) {
          try {
            values.date = values['date'].format('YYYY-MM-DD')
            await editFollowupPlan({
              ...values,
              id,
            })
            this.$emit('succeed')
            this.$message.success('操作成功！')
            this.close()
          } catch (error) {
            this.$message.error(error)
          } finally {
            this.loading = false
          }
        } else {
          setTimeout(() => {
            this.loading = false
          }, 600)
        }
      })
    },
    uploadImage: function (e) {
      let formdata = new FormData()
      formdata.append('file', e.file)
      axios({
        url: `${this.env}/file/upload`,
        method: 'post',
        data: formdata,
      }).then(({ status, data }) => {
        if (status == 200 && data.code == 1) {
          this.images.push(`${data.data}`)
        }
      })
    },
    close() {
      this.visible = false
      this.form.resetFields()
    },
  },
}
</script>
<style lang="less" scoped>
.shadow {
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.2);
}
</style>